package GUI.Productos;

import java.rmi.RemoteException;
import java.util.ArrayList;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

import dbExceptions.DataBaseErrorException;
import service.ServiceFacade;
import service.article.exceptions.ProxyException;
import service.article.interfaces.ArticleProxyMgt;
import GUI.Administracion.Exceptions.ExcepcionesMenuVentas;
import article.entities.ArticleVO;
import article.entities.CategoryVO;
import article.entities.IngredientVO;
import article.exceptions.BadCategoryException;
import article.exceptions.IncompleteArticleException;
import article.exceptions.MissingArticleException;
import article.exceptions.MissingCategoryException;

public class NewProducto extends Dialog {

	protected Object result;
	protected Shell shell;
	private Text nombre;
	private Text precio;
	private Text descripcion;
	ArrayList<IngredientVO> ings;
	Label error;
	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public NewProducto(Shell parent, int style, ArrayList<IngredientVO> ing) {
		super(parent, style);
		setText("SWT Dialog");
		ings= ing;
	}

	/**
	 * Open the dialog.
	 * @return the result
	 * @throws ProxyException 
	 * @throws RemoteException 
	 */
	public Object open() throws RemoteException, ProxyException {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 * @throws ProxyException 
	 * @throws RemoteException 
	 */
	private void createContents() throws RemoteException, ProxyException {
		shell = new Shell(getParent(), getStyle());
		shell.setSize(450, 329);
		shell.setText(getText());
		shell.setLayout(new FormLayout());
		
		Label lblNombre = new Label(shell, SWT.NONE);
		FormData fd_lblNombre = new FormData();
		fd_lblNombre.top = new FormAttachment(0, 50);
		fd_lblNombre.left = new FormAttachment(0, 35);
		lblNombre.setLayoutData(fd_lblNombre);
		lblNombre.setText("Nombre");
		
		Label lblPreciio = new Label(shell, SWT.NONE);
		FormData fd_lblPreciio = new FormData();
		fd_lblPreciio.top = new FormAttachment(lblNombre, 20);
		fd_lblPreciio.left = new FormAttachment(lblNombre, 0, SWT.LEFT);
		lblPreciio.setLayoutData(fd_lblPreciio);
		lblPreciio.setText("Preciio");
		
		Label lblCategoria = new Label(shell, SWT.NONE);
		FormData fd_lblCategoria = new FormData();
		fd_lblCategoria.top = new FormAttachment(lblPreciio, 20);
		fd_lblCategoria.left = new FormAttachment(lblNombre, 0, SWT.LEFT);
		lblCategoria.setLayoutData(fd_lblCategoria);
		lblCategoria.setText("Categoria");
		
		Label lblDescripcion = new Label(shell, SWT.NONE);
		FormData fd_lblDescripcion = new FormData();
		fd_lblDescripcion.top = new FormAttachment(lblCategoria, 20);
		fd_lblDescripcion.left = new FormAttachment(lblNombre, 0, SWT.LEFT);
		lblDescripcion.setLayoutData(fd_lblDescripcion);
		lblDescripcion.setText("Descripcion");
		
		Label lblIngredientes = new Label(shell, SWT.NONE);
		FormData fd_lblIngredientes = new FormData();
		fd_lblIngredientes.top = new FormAttachment(lblDescripcion, 20);
		fd_lblIngredientes.left = new FormAttachment(lblNombre, 0, SWT.LEFT);
		lblIngredientes.setLayoutData(fd_lblIngredientes);
		lblIngredientes.setText("Ingredientes");
		
		nombre = new Text(shell, SWT.BORDER);
		FormData fd_nombre = new FormData();
		fd_nombre.top = new FormAttachment(lblNombre, -3, SWT.TOP);
		fd_nombre.left = new FormAttachment(lblNombre, 50);
		nombre.setLayoutData(fd_nombre);
		
		precio = new Text(shell, SWT.BORDER);
		fd_nombre.right = new FormAttachment(precio, 0, SWT.RIGHT);
		FormData fd_precio = new FormData();
		fd_precio.left = new FormAttachment(lblPreciio, 57);
		precio.setLayoutData(fd_precio);
		
		final Combo combo = new Combo(shell, SWT.NONE);
		fd_precio.right = new FormAttachment(combo, 0, SWT.RIGHT);
		fd_precio.bottom = new FormAttachment(combo, -12);
		FormData fd_combo = new FormData();
		fd_combo.top = new FormAttachment(0, 110);
		fd_combo.right = new FormAttachment(lblCategoria, 149, SWT.RIGHT);
		fd_combo.left = new FormAttachment(lblCategoria, 41);
		combo.setLayoutData(fd_combo);
		final ArticleProxyMgt uso = ServiceFacade.getInstance().getArticleProxyMgt();
		final CategoryVO[] cats = uso.getCategories();
		
		for (int i=0;i<cats.length;i++){
			combo.add(cats[i].getCat());
		}
		descripcion = new Text(shell, SWT.BORDER);
		FormData fd_descripcion = new FormData();
		fd_descripcion.right = new FormAttachment(lblDescripcion, 242, SWT.RIGHT);
		fd_descripcion.top = new FormAttachment(combo, 20);
		fd_descripcion.left = new FormAttachment(lblDescripcion, 30);
		descripcion.setLayoutData(fd_descripcion);
		
		Label ingredientes = new Label(shell, SWT.NONE);
		FormData fd_ingredientes = new FormData();
		fd_ingredientes.top = new FormAttachment(lblIngredientes, 0, SWT.TOP);
		fd_ingredientes.left = new FormAttachment(nombre, 0, SWT.LEFT);
		ingredientes.setLayoutData(fd_ingredientes);
		if(ings==null){
		ingredientes.setText("");
		}
		else{
			String n = "";
			for (int i=0;i<ings.size();i++){
				n = n + ings.get(i).getIngr() + " ";
			}
			ingredientes.setText(n);
		}
		Button btnSeleccionarIngredientes = new Button(shell, SWT.NONE);
		FormData fd_btnSeleccionarIngredientes = new FormData();
		fd_btnSeleccionarIngredientes.top = new FormAttachment(ingredientes, 6);
		fd_btnSeleccionarIngredientes.left = new FormAttachment(nombre, 0, SWT.LEFT);
		btnSeleccionarIngredientes.setLayoutData(fd_btnSeleccionarIngredientes);
		btnSeleccionarIngredientes.setText("Seleccionar Ingredientes");
		
		Button btnAceptar = new Button(shell, SWT.NONE);
		btnAceptar.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String n = nombre.getText();
				double p = Integer.parseInt(precio.getText());
				String desc = descripcion.getText();
				String categ = combo.getText();
				CategoryVO cat = null ;
				for (int i=0;i<cats.length;i++){
					if(categ.equalsIgnoreCase(cats[i].getCat())){
						cat = cats[i];
					}
				}
				IngredientVO [] ingred = new IngredientVO[ings.size()];
				for (int j = 0; j<ings.size();j++){
					ingred[j]=ings.get(j);
				}
				ArticleVO art = new ArticleVO(n,desc,p,ingred,cat);
				try {
					try {
						uso.addArticle(art);
					} catch (MissingCategoryException e1) {
						error.setText("No se selecciono categoria de articulo");
						
						
					} catch (DataBaseErrorException e1) {
						ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
						ventana.open();
						shell.dispose();
						e1.printStackTrace();
					} catch (BadCategoryException e1) {
						ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
						ventana.open();
						shell.dispose();
						e1.printStackTrace();
					} catch (MissingArticleException e1) {
						ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
						ventana.open();
						shell.dispose();
						e1.printStackTrace();
					}
				} catch (RemoteException e1) {
					ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
					ventana.open();
					shell.dispose();
					e1.printStackTrace();
				} catch (IncompleteArticleException e1) {
					ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "No se ingresaron los datos de manera correcta");
					ventana.open();
					shell.dispose();
					e1.printStackTrace();
				} catch (ProxyException e1) {
					ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
					ventana.open();
					shell.dispose();
					e1.printStackTrace();
				}
						
			}
		});
		FormData fd_btnAceptar = new FormData();
		fd_btnAceptar.bottom = new FormAttachment(100, -10);
		fd_btnAceptar.left = new FormAttachment(0, 184);
		btnAceptar.setLayoutData(fd_btnAceptar);
		btnAceptar.setText("ACEPTAR");
		
		error = new Label(shell, SWT.NONE);
		FormData fd_error = new FormData();
		fd_error.right = new FormAttachment(descripcion, 0, SWT.RIGHT);
		fd_error.left = new FormAttachment(lblNombre, 0, SWT.LEFT);
		fd_error.bottom = new FormAttachment(btnAceptar, -9);
		error.setLayoutData(fd_error);

	}
}
